<template>
  <div>
    <!-- <dv-active-ring-chart :config="config" style="width:300px;height:300px" /> -->
    <el-carousel trigger="click" height="250px" direction="vertical">
      <el-carousel-item>
        <div class="pie" id="pie" style="width: 100%; height: 250px"></div>
      </el-carousel-item>
      <el-carousel-item class="box-end">
        <!-- <div class="bar" id="bar" style="width: 100%;height:250px;"></div> -->
        <div class="box-container">
          <dv-decoration-9 style="width: 150px; height: 150px; margin: 15px">
            <p>
              <span style="font-weight: bold; font-size: 26px">{{
                length
              }}</span
              ><br />次
            </p>
          </dv-decoration-9>
        </div>
        <div class="box-container">
          <p>大于六级：</p>
          <p>
            <span style="font-weight: bold; font-size: 22px; color: red">{{
              l6_1
            }}</span
            >&nbsp;次
          </p>
          <p>小于六级：</p>
          <p>
            <span style="font-weight: bold; font-size: 22px">{{
              length - l6_1
            }}</span
            >&nbsp;次
          </p>
        </div>
        <h2 style="margin-top: 0">{{ year }}年</h2>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
import { getLevel_ } from "@/utils";
import { getDataAll } from "@/api/index";
export default {
  name: "Test1LeftCenter",

  data() {
    return {
      config: {
        data: [
          {
            name: "周口",
            value: 55,
          },
          {
            name: "南阳",
            value: 120,
          },
          {
            name: "西峡",
            value: 78,
          },
          {
            name: "驻马店",
            value: 66,
          },
          {
            name: "新乡",
            value: 80,
          },
        ],
      },
      pieChart: null,
      barChart: null,
      points: [],
      levalInfo: [],
      index: 0,
      year: 2019,
      length: 0,
      l6_1: 0,
    };
  },
  computed: {
    getAllData() {
      return this.$store.state.points;
    },
  },
  watch: {
    getAllData: {
      handler(newVal) {
        if (newVal.length > 0) {
          this.points = newVal;
          this.getLevalInfo(newVal);
        }
      },
      deep: true,
      immediate: true,
    },
  },

  mounted() {
    // this.getDataAll();
    let pie = document.getElementById("pie");
    let st = setInterval(() => {
      if (pie && this.levalInfo.length > 0) {
        this.initPie();
        // this.initBar()
        clearInterval(st);
      }
    }, 600);

    // setTimeout(() => {
    //   this.initPie()

    // }, 1500);
  },

  methods: {
    initPie() {
      var chartDom = document.getElementById("pie");
      this.pieChart = this.$echarts.init(chartDom);
      var option;

      // var index = 0;

      option = {
        // title: {
        //   text: 'aaa',
        //   textStyle: {
        //     textAlign: 'center',
        //     color: '#1dc1f5'
        //   }
        // },
        tooltip: {
          trigger: "item",
        },
        toolbox: {
          show: true,
          feature: {
            saveAsImage: {
              show: true,
            },
          },
        },

        legend: {
          // show:false,
          top: "center",
          left: "0",
          orient: "vertical",
          textStyle: {
            color: "#1dc1f5",
          },
        },
        series: [
          {
            name: "地震震级",
            type: "pie",
            radius: ["40%", "70%"],
            // roseType: 'radius',
            // top:'30px',
            left: "50px",
            itemStyle: {
              borderRadius: 10,
              borderWidth: 20,
            },
            label: {
              show: false,
              position: "center",
              color: "#1dc1f5",
              fontSize: 18,
            },

            labelLine: {
              show: false,
            },
            // data: [
            //   { value: 1048, name: '≤3' },
            //   { value: 735, name: '≤4.5' },
            //   { value: 580, name: '≤6' },
            //   { value: 484, name: '≤7' },
            //   { value: 300, name: '≤8' },
            //   { value: 300, name: '8≥' },
            // ]
          },
        ],
        graphic: [
          {
            type: "text",
            id: "year",
            left: "50%",
            top: "center",
            z: 10,
            style: {
              text: "2019 年",
              fill: "#1dc1f5",
              fontSize: 16,
              fontWeight: "bold",
            },
          },
        ],
      };

      this.pieChart.setOption(option, this.index);
      setInterval(() => {
        this.index++;
        if (this.index == this.levalInfo.length) {
          this.index = 0;
        }

        this.setPieOption(option, this.index);
      }, 4000);

      // option && this.pieChart.setOption(option);
    },
    initBar() {
      var bar = document.getElementById("bar");
      this.barChart = this.$echarts.init(bar);

      var option;

      option = {
        title: {
          text: "",
          textStyle: {
            textAlign: "center",
            color: "#1dc1f5",
          },
        },
        legend: {},
        xAxis: {
          type: "category",
          data: ["≤3", "≤4.5", "≤6", "≤7", "≤8", "≥8"],
          axisLine: {
            lineStyle: {
              color: "#1dc1f5",
            },
          },
          axisLabel: {
            color: "#1dc1f5",
          },
        },
        yAxis: {
          type: "value",
          // splitNumber: 3,

          axisLine: {
            show: false,
            lineStyle: {
              color: "#1dc1f5",
            },
          },
          axisLabel: {
            color: "#1dc1f5",
          },

          spliteLine: false,
        },
        series: [
          {
            // data: [120, 200, 150, 80, 70, 110],
            type: "bar",
            itemStyle: {
              normal: {
                //这里是重点
                color: function (params) {
                  //注意，如果颜色太少的话，后面颜色不会自动循环，最好多定义几个颜色
                  var colorList = [
                    "#5470c6",
                    "#91cc75",
                    "#fac858",
                    "#ee6666",
                    "#73c0de",
                    "#3ba272",
                    "#ca8622",
                  ];
                  return colorList[params.dataIndex];
                },
              },
            },
          },
        ],
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
      };
      this.barChart.setOption(option, this.index);
      setInterval(() => {
        this.index++;
        if (this.index == this.levalInfo.length) {
          this.index = 0;
        }

        this.setBarOption(option, this.index);
      }, 4000);

      // option && this.barChart.setOption(option);
    },
    getLevalInfo(d) {
      for (let i = 0; i < d.length; i++) {
        const element = d[i];
        this.levalInfo.push(getLevel_(element));
      }
    },
    setPieOption(option, index) {
      option.series[0].data = [
        // { value: this.levalInfo[index][3].length, name: "≤3" },
        // { value: this.levalInfo[index][4.5].length, name: "≤4.5" },
        // { value: this.levalInfo[index][6].length, name: "≤6" },
        // { value: this.levalInfo[index][7].length, name: "≤7" },
        // { value: this.levalInfo[index][8].length, name: "≤8" },
        // { value: this.levalInfo[index][8.1].length, name: "8≥" },
        { value: this.levalInfo[index]["m1"].length, name: "弱震" },
        { value: this.levalInfo[index]["m2"].length, name: "有感地震" },
        { value: this.levalInfo[index]["m3"].length, name: "中强震" },
        { value: this.levalInfo[index]["m4"].length, name: "强震" },
      ];
      this.length =
        // this.levalInfo[index][3].length +
        // this.levalInfo[index][4.5].length +
        // this.levalInfo[index][6].length +
        // this.levalInfo[index][7].length +
        // this.levalInfo[index][8].length +
        // this.levalInfo[index][8.1].length;
        this.levalInfo[index]["m1"].length +
        this.levalInfo[index]["m2"].length +
        this.levalInfo[index]["m3"].length +
        this.levalInfo[index]["m4"].length;
      this.year = Object.values(this.levalInfo[index])[4];
      this.l6_1 = this.levalInfo[index]["m4"].length;
      // this.levalInfo[index][7].length +
      // this.levalInfo[index][8].length +
      // this.levalInfo[index][8.1].length;

      option.graphic[0].style.text =
        Object.values(this.levalInfo[index])[4] + "年";
      option && this.pieChart.setOption(option);
    },
    setBarOption(option, index) {
      option.series[0].data = [
        this.levalInfo[index]["m1"].length,
        this.levalInfo[index]["m2"].length,
        this.levalInfo[index]["m3"].length,
        this.levalInfo[index]["m4"].length,
      ];
      option.title.text = Object.values(this.levalInfo[index])[4] + "年";

      option && this.barChart.setOption(option);
    },
    async getDataAll() {
      const { data: res } = await getDataAll(2011, 2012);
      this.getLevalInfo(res);
      this.$store.commit("setPoints", res);
    },
  },
};
</script>

<style lang="less" scoped>
.box-end {
  margin-top: 10px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-content: space-around;
  color: #1dc1f5;
}

.box-container {
  // position: absolute;
  display: inline-block;
  top: 0;
  margin-top: 0;
  width: 50%;
  color: #1dc1f5;
}
</style>
